package com.sina.shop.manage.controller.order;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.order.Order;
import com.sina.shop.domain.order.Status;
import com.sina.shop.manage.service.order.OrderManageService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-12-17:58
 */
@Controller
@RequestMapping("orderManage")
public class OrderManageController extends BaseController {


    @Autowired
    @Qualifier("orderManageService")
    private OrderManageService orderManageService;

    @RequestMapping(value = "getOrderList", method = RequestMethod.GET)
    public ModelAndView getOrderList(@Valid Order order) {
        LOG.info("order:{}", order);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);
        Long count[] = new Long[1];
        List<Map<String, Object>> permissionList = orderManageService.getOrderList(order, count);

        Map<String, Object> data = new HashMap<>(4);
        data.put("recordsTotal", count[0]);
        data.put("recordsFiltered", count[0]);
        data.put("data", permissionList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "deleteOrder", method = RequestMethod.POST)
    public ModelAndView deleteOrder(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            orderManageService.deleteOrder(id);
            result.put(CODE, OK);
            result.put(MSG, "删除订单成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "updateOrder", method = RequestMethod.POST)
    public ModelAndView updateOrder(@Valid Order order) {

        LOG.info("order:{}", order);


        if (order.getStatus().equals(Status.PAID)) {
            order.setPayTime(LocalDateTime.now());
        }
        if (order.getStatus().equals(Status.UN_PAID)) {
            order.setPayTime(null);
        }

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            orderManageService.updateOrder(order);
            result.put(CODE, OK);
            result.put(MSG, "更新订单成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "saveOrder", method = RequestMethod.POST)
    public ModelAndView saveOrder(@Valid Order order) {

        LOG.info("order:{}", order);

        /*创建时间、修改时间、创建人*/
        order.setCreateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            orderManageService.saveOrder(order);
            result.put(CODE, OK);
            result.put(MSG, "新增订单成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "getOrderListToHtml", method = RequestMethod.GET)
    private ModelAndView getOrderListToHtml() {

        ModelAndView mv = new ModelAndView("order/orderList");

        return mv;
    }
}
